package com.wtone.platform.gat.images.gat.service.impl;

import com.wtone.platform.gat.images.gat.core.Constants;
import com.wtone.platform.gat.images.gat.core.Response;
import com.wtone.platform.gat.images.gat.entity.Imagedb;
import com.wtone.platform.gat.images.gat.mapper.ImagedbMapper;
import com.wtone.platform.gat.images.gat.model.ImageListObject;
import com.wtone.platform.gat.images.gat.model.ImageObj;
import com.wtone.platform.gat.images.gat.model.ResponseStatus;
import com.wtone.platform.gat.images.gat.model.ResponseStatusListModeObject;
import com.wtone.platform.gat.images.gat.service.ImageService;
import com.wtone.platform.gat.images.gat.utils.BenUtils;
import com.wtone.platform.gat.images.gat.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: aiLin
 * @Date: 2020/10/13 15:11
 * @desc: 图像相关接口
 */
@Slf4j
@Service
public class ImageServiceImpl implements ImageService {


    @Autowired
    HttpServletRequest request;

    @Autowired
    private ImagedbMapper imagedbMapper;

    /**
     * 批量图像新增接口
     * @param imageListObject
     * @return
     */
    @Override
    public ResponseStatusListModeObject addImages(ImageListObject imageListObject) {

        String reqUrl = "/VIID/Images";
        String userIdentify = request.getHeader(Constants.REQTOKEN);
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();
        ResponseStatus r = new ResponseStatus();
        if(imageListObject==null){
            r = Response.getResponOk(userIdentify,reqUrl,"对象传入不能为空");
        }
        List<ImageObj> imageObjList = imageListObject.getImageListObject().getImageObjList();
        if(imageObjList==null || imageObjList.size()<=0){
            r = Response.getResponOk(userIdentify,reqUrl,"图片对象传入不能为空");
        }
        for(int i=0;i<imageObjList.size();i++){
            ImageObj imageObj = imageObjList.get(i);
            Imagedb imagedb = null;
            try {
                imagedb = (Imagedb) BenUtils.beanTobeanLowerCase(imageObj, Imagedb.class);
                imagedb.setCreateBy("admin");
                imagedb.setRemark(DateUtils.getDate()+"图像加入");
                imagedb.setCreateTime(DateUtils.getNowDate());
                String faceID = imageObj.getFaceList().getFaceObject().getFaceList().get(0).getFaceID();
                imagedb.setFaceID(faceID);
                imagedb.setImageid(imageObj.getImage().getImageID()!=null ? imageObj.getImage().getImageID() : "");
                int mark = imagedbMapper.insertImagedb(imagedb);
                if(mark>=1){
                    r = Response.getResponOk(userIdentify,reqUrl,"图片列表加入成功");
                }else{
                    r = Response.getResponOk(userIdentify,reqUrl,"图片列表加入异常");
                }
            } catch (Exception e) {
                if(String.valueOf(e).contains("唯一")){
                    log.error("人脸数据：{}，已存在，不重新记录",imagedb.getFaceID());
                    r = Response.getResponOk(imagedb.getFaceID(),reqUrl,"人脸数据："+imagedb.getFaceID()+"，已存在，不重新记录");
                }else{
                    r = Response.getResponOk(imagedb.getFaceID(),reqUrl,"人脸数据："+imagedb.getFaceID()+"，异常，存入记录表");
                }
            }
        }
        resp3.add(r);
        return Response.ok(resp3);
    }

    /**
     * 图像查询
     * @param FaceID
     * @param ImageID
     * @return
     */
    @Override
    public List<Imagedb> queryImages(String FaceID, String ImageID) {
        Imagedb imagedb = new Imagedb();
        imagedb.setImageid(ImageID!=null ? ImageID : "");
        imagedb.setFaceID(FaceID!=null ? FaceID : "");
        List<Imagedb> imagedbs = imagedbMapper.selectImagedbList(imagedb);
        return imagedbs;
    }
}
